package com.lg.atp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.atp.dao.CaseInfoDao;
import com.lg.atp.dao.CaseStepDao;
import com.lg.atp.dao.NumberInfoDao;
import com.lg.atp.entity.TblCaseInfo;
import com.lg.atp.entity.TblCaseStep;
import com.lg.atp.entity.TblNumberInfo;
import com.lg.atp.service.ICaseService;

@Service("iCaseService")
public class CaseServiceImpl implements ICaseService{

	@Autowired
	private CaseInfoDao caseInfoDao;
	@Autowired
	private CaseStepDao caseStepDao;
	@Autowired
	private NumberInfoDao numberInfoDao;
	
	private final String caseNumberCode = "CASE";
	
	@Override
	public PageInfo<TblCaseInfo> selectAllCase(Map<String,Object> map) {
		Integer pageIndex = Integer.valueOf(map.get("pageIndex").toString());
		Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
		PageHelper.startPage(pageIndex, pageSize);
		List<TblCaseInfo> caseList = caseInfoDao.selectAllCase();
		PageInfo<TblCaseInfo> list = new PageInfo<TblCaseInfo>(caseList);
		return list;
	}

	
	@Transactional
	@Override
	public Long saveCase(TblCaseInfo caseInfo) {
		
		String caseNumber = this.generateNumber();
		caseInfo.setCaseNumber(caseNumber);
		List<TblCaseStep> stepList = caseInfo.getStepList();
		caseInfoDao.saveCase(caseInfo);
		Long	caseId = caseInfo.getId();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("caseId", caseId);
		map.put("userId", caseInfo.getChangeUserId());
		map.put("list", stepList);
		caseStepDao.saveSteps(map);
		return caseId;
	}


	@Transactional
	@Override
	public void updateCase(TblCaseInfo caseInfo) {
		
		Long caseId = caseInfo.getId();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", caseInfo.getChangeUserId());
		map.put("caseId", caseId);
		caseStepDao.setStepInvalidate(map);
		
		List<TblCaseStep> stepList = caseInfo.getStepList();
		caseInfoDao.updateCase(caseInfo);
		Map<String,Object> map1 = new HashMap<String,Object>();
		map1.put("caseId", caseId);
		map1.put("userId", caseInfo.getChangeUserId());
		map1.put("list", stepList);
		caseStepDao.saveSteps(map1);
	}


	@Override
	public TblCaseInfo getCaseById(Long id) {
		return caseInfoDao.selectCaseWithStep(id);
	}


	@Override
	public String generateNumber() {
		String prefixCode = DateFormatUtils.format(new Date(), "yyyyMMdd");
		TblNumberInfo number = new TblNumberInfo();
		number.setNumberCode(caseNumberCode);
		number.setPrefixCode(prefixCode);
		Long maxNum = numberInfoDao.selectMaxNum(number);
		if(maxNum == null){
			number.setCurrentNumber(1l);
			numberInfoDao.saveNum(number);
			int randomInt =(int)(Math.random()*1000);
			return prefixCode+1+String.format("%03d", randomInt);
		}else{
			Long newNum = maxNum+1;
			number.setCurrentNumber(newNum);
			numberInfoDao.updateNum(number);
			int randomInt =(int)(Math.random()*1000);
			return prefixCode+newNum+String.format("%03d", randomInt);
		}
	}


	@Override
	public PageInfo<TblCaseInfo> selectCaseByPara(Map<String, Object> map) {
		Integer pageIndex = Integer.valueOf(map.get("pageIndex").toString());
		Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
		PageHelper.startPage(pageIndex, pageSize);
		List<TblCaseInfo> caseList = caseInfoDao.selectCaseByPara(map);
		PageInfo<TblCaseInfo> list = new PageInfo<TblCaseInfo>(caseList);
		return list;
	}


	@Override
	public List<TblCaseStep> getStepByCaseId(Long caseId) {
		return caseStepDao.selectStepByCaseId(caseId);
	}

}
